package com.crm.action;

import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import com.crm.pojo.User;
import com.crm.service.UserService;

@Namespace("/")
public class AppAction implements SessionAware {
	
	private Map<String,Object> session;
	
	private User user;
	private String code;
	
	
	@Autowired
	private UserService userService;
	
	@Action("index")
	public String execute(){
		return "success";
	}
	@Action("main")
	public String main(){
		return "success";
	}
	
	@Action(value="login",results={
			@Result(name="login",type="redirectAction",params={"actionName","index","namespace","/"}),
			@Result(name="success",type="redirectAction",params={"actionName","main","namespace","/"})
	})
	public String login(){
		User u=userService.login(user);
		//User u = userService.findByUserName("tom");
		if(u==null){
			code="001";
			return "login";
		}else{
			session.put("user", u);
			return "success";
		}
	}
	
	
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	
	

}
